<?php

  define('BASE_ROOT_PATH', dirname(dirname(__FILE__)));
	include_once( BASE_ROOT_PATH . "/word/db/DBMySQL.php");  
	include 'apns.php';
	
	$memberId = $_REQUEST['id'];	
	if(empty($memberId)) {
	  $msg = json_encode("用户id不能为空");
		echo '{"code":"1","msg":'.$msg.'}';
		exit();
	}
	$memberId = str_replace('\'', '\'\'', $memberId);
	
	$to = $_REQUEST['to'];	
	if(empty($to)) {
	  $msg = json_encode("接收用户id不能为空");
		echo '{"code":"1","msg":'.$msg.'}';
		exit();
	}
	$to = str_replace('\'', '\'\'', $to);
	
	$content = $_REQUEST['content'];
	if(empty($content)) {
	  $msg = json_encode("内容不能为空");
		echo '{"code":"1","msg":'.$msg.'}';
		exit();
	}
	$content = str_replace('\'', '\'\'', $content);
	
	// 查询用户
	$db = new DBMySQL();
	$rs = $db->Query("select member_id,nick from t_member where member_id in ($to,$memberId) limit 2");
	$sender;
	$pushContent;
	if(empty($rs) || 2 > count($rs)) { 
	  $msg = json_encode("用户不存在");
	  echo '{"code":"1","msg":'.$msg.'}';
		exit();	  
	}
	else {
		
		
		$rs = $db->Query("select member_id,nick from t_member where member_id in ($memberId) limit 2");
		$sender = $rs[0]['nick'];
		$pushContent = $sender."：".$content;
	
	}
	
	date_default_timezone_set("UTC");
	$timestamp=time();
	//echo date("Y-m-d H:i:s",$timestamp);
	$db->Insert("insert into t_msg(from_id,to_id,content,create_time) values($memberId,$to,'$content',$timestamp)");
	
	if(!empty($memberId)) { // 添加成功
	  echo '{"code":"0","msg":""}';
	   //echo $timestamp;
	   //echo "insert into t_msg(from_id,to_id,content,create_time) values($memberId,$to,'$content',$timestamp)";
	}
	$tokenRs = $db->Query("select token from t_token where member_id in ($to) limit 1");
	if(!empty($tokenRs)){
		$token = $tokenRs[0][0];
		pushNotification($token, $pushContent, 0);	
	}
	
?>